/*
 * cadProdutoJInternalFrame.java
 *
 * Created on 27/10/2009, 21:03:56
 */
package br.com.fatecpg.zanotti.jframes.consultas;

import br.com.fatecpg.zanotti.acessobanco.conTableCliente;
import br.com.fatecpg.zanotti.acessobanco.dmlCliente;
import br.com.fatecpg.zanotti.jframes.MDI;
import br.com.fatecpg.zanotti.jframes.cadastros.cadClienteJInternalFrame;
import br.com.fatecpg.zanotti.jframes.cadastros.cadVendaJInternalFrame;
import br.com.fatecpg.zanotti.modelo.Cliente;

/**
 * Janela Consulta de Cliente
 * @author Luiz Zanotti
 */
public class conClienteJInternalFrame extends javax.swing.JInternalFrame {

    /** 
     * Cria um novo cadProdutoJInternalFrame
     */
    public conClienteJInternalFrame() {
        initComponents();
        b = false;
        conClijTable.getColumnModel().getColumn(1).setMaxWidth(90);
        conClijTable.getColumnModel().getColumn(1).setMinWidth(90);
        conClijTable.getColumnModel().getColumn(0).setMaxWidth(300);
        conClijTable.getColumnModel().getColumn(0).setMinWidth(210);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        conClijTable = new javax.swing.JTable();
        buscarjTextField = new javax.swing.JTextField();
        nome_cpfjComboBox = new javax.swing.JComboBox();
        buscarjButton = new javax.swing.JButton();
        pesquisarjLabel = new javax.swing.JLabel();
        opcoesjPanel = new javax.swing.JPanel();
        alterarjButton = new javax.swing.JButton();
        vendajButton = new javax.swing.JButton();
        excluirjButton = new javax.swing.JButton();
        novojButton = new javax.swing.JButton();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setIconifiable(true);
        setTitle("Consulta de Clientes");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        conClijTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Nome", "CPF"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                true, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        conClijTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        conClijTable.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(conClijTable);
        conClijTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        conClijTable.getColumnModel().getColumn(0).setResizable(false);
        conClijTable.getColumnModel().getColumn(1).setResizable(false);

        nome_cpfjComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nome", "CPF" }));

        buscarjButton.setText("Buscar");
        buscarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarjButtonActionPerformed(evt);
            }
        });

        pesquisarjLabel.setText("Pesquisar por:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(pesquisarjLabel)
                        .addGap(6, 6, 6)
                        .addComponent(buscarjTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(nome_cpfjComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(buscarjButton)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buscarjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pesquisarjLabel)
                    .addComponent(buscarjButton)
                    .addComponent(nome_cpfjComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        alterarjButton.setText("Alterar");
        alterarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                alterarjButtonActionPerformed(evt);
            }
        });

        vendajButton.setText("Venda");
        vendajButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vendajButtonActionPerformed(evt);
            }
        });

        excluirjButton.setText("Excluir");
        excluirjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                excluirjButtonActionPerformed(evt);
            }
        });

        novojButton.setText("Novo");
        novojButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                novojButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout opcoesjPanelLayout = new javax.swing.GroupLayout(opcoesjPanel);
        opcoesjPanel.setLayout(opcoesjPanelLayout);
        opcoesjPanelLayout.setHorizontalGroup(
            opcoesjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(opcoesjPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(opcoesjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(novojButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(excluirjButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(vendajButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(alterarjButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        opcoesjPanelLayout.setVerticalGroup(
            opcoesjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(opcoesjPanelLayout.createSequentialGroup()
                .addComponent(alterarjButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(vendajButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(excluirjButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(novojButton)
                .addContainerGap(186, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(opcoesjPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(opcoesjPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buscarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarjButtonActionPerformed
        System.out.println("Buscou Cliente");
        conClijTable.removeAll();

        if (nome_cpfjComboBox.getSelectedItem() == "Nome") {
            conTableCliente con = new conTableCliente();
            conClijTable = con.ConsultaTabelaPorNome(conClijTable, buscarjTextField.getText());
        } else {
            conTableCliente con = new conTableCliente();
            conClijTable = con.ConsultaTabelaPorCPF(conClijTable, buscarjTextField.getText());
        }
    }//GEN-LAST:event_buscarjButtonActionPerformed

    private void novojButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_novojButtonActionPerformed
        System.out.println("Abriu novo cadastro de cliente");

        MDI.getInstancia().cadcliVis(true);
        cadClienteJInternalFrame.getInstancia().atualizaCDCLIENTE();
        this.hide();

    }//GEN-LAST:event_novojButtonActionPerformed
   
    /**
     * Método utilizado para retornar a variável boolean b.
     * @return boolean
     */

    public boolean isConsultado() {
        return b;
    }
    boolean b;
    private static conClienteJInternalFrame p;
    /**
     * Método que vai retornar uma instância Singleton de conClienteJInternalFrame.
     */
    public static conClienteJInternalFrame getInstancia() {
        if (p == null) {
            p = new conClienteJInternalFrame();
        }
        return p;
    }
    Cliente cli;
    private void alterarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_alterarjButtonActionPerformed
        if (conClijTable.getSelectedRow() < 0) {
            javax.swing.JOptionPane.showMessageDialog(null, "Nenhuma linha selecionada!", "Atenção", 2);
        } else {
            this.setVisible(false);
            String str = conClijTable.getValueAt(conClijTable.getSelectedRow(), 1).toString();

            cli = new conTableCliente().ConsultaObjetoPorLinha(str);

            cadClienteJInternalFrame.getInstancia().setVisible(true);
            cadClienteJInternalFrame.getInstancia().setNomejTextField(cli.getNMCLIENTE());
            cadClienteJInternalFrame.getInstancia().setRgjTextField(cli.getRGCLIENTE());
            cadClienteJInternalFrame.getInstancia().setDatajFormattedTextField(cli.getDTNASCIMENTO());
            cadClienteJInternalFrame.getInstancia().setCpfjFormattedTextField(cli.getCPFCLIENTE());
            cadClienteJInternalFrame.getInstancia().setEndBairrojTextField(cli.getNMENDBAIRRO());
            cadClienteJInternalFrame.getInstancia().setEndCEPjTextField(cli.getNMENDCEP());
            cadClienteJInternalFrame.getInstancia().setEndCidadejTextField(cli.getNMENDCIDADE());
            cadClienteJInternalFrame.getInstancia().setEndCompjTextField(cli.getNMENDCOMP());
            cadClienteJInternalFrame.getInstancia().setEndEmailjTextField(cli.getNMEMAIL());
            cadClienteJInternalFrame.getInstancia().setEndLogjTextField(cli.getNMENDLOGRADOURO());
            cadClienteJInternalFrame.getInstancia().setEndNumjTextField(cli.getNMENDNUMERO());
            cadClienteJInternalFrame.getInstancia().setEndUFjTextField(cli.getSGENDESTADO());
            cadClienteJInternalFrame.getInstancia().setTelefonejFormattedTextField(cli.getCDTELEFONE());
            cadClienteJInternalFrame.getInstancia().setLojajComboBox(cli.getCDLOJA());
            cadClienteJInternalFrame.getInstancia().setCdclientejTextField(cli.getCDCLIENTE());

            cadClienteJInternalFrame.getInstancia().setTitle("Alteração de Cadastro de Cliente");
            cadClienteJInternalFrame.getInstancia().setCadastrarJButton("Salvar");

        }
    }//GEN-LAST:event_alterarjButtonActionPerformed

    private void excluirjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_excluirjButtonActionPerformed
        if (conClijTable.getSelectedRow() < 0) {
            javax.swing.JOptionPane.showMessageDialog(null, "Nenhuma linha selecionada!", "Atenção", 2);
        } else {
            String str = conClijTable.getValueAt(conClijTable.getSelectedRow(), 1).toString();
            Cliente cliented = new conTableCliente().ConsultaObjetoPorLinha(str);
            dmlCliente.deletar(cliented.getCDCLIENTE());
            atualizaTable();
        }
    }//GEN-LAST:event_excluirjButtonActionPerformed

    private void vendajButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vendajButtonActionPerformed
        if (conClijTable.getSelectedRow() < 0) {
            javax.swing.JOptionPane.showMessageDialog(null, "Nenhuma linha selecionada!", "Atenção", 2);
        } else {
            this.setVisible(false);
            String str = conClijTable.getValueAt(conClijTable.getSelectedRow(), 1).toString();

            cli = new conTableCliente().ConsultaObjetoPorLinha(str);

            cadVendaJInternalFrame.getInstancia().setVisible(true);
            cadVendaJInternalFrame.getInstancia().setNomeClientejTextField(Cliente.getInstancia().getNMCLIENTE());
            cadVendaJInternalFrame.getInstancia().setCliente(cli.getCDCLIENTE(), cli.getCDLOJA());
            cadVendaJInternalFrame.getInstancia().setEnd1jTextArea(Cliente.getInstancia().getNMENDLOGRADOURO() + ", " + Cliente.getInstancia().getNMENDNUMERO() + " - " + Cliente.getInstancia().getNMENDCOMP() + " - " + Cliente.getInstancia().getNMENDBAIRRO() + ", " + Cliente.getInstancia().getNMENDCIDADE() + " \n CEP: " + Cliente.getInstancia().getNMENDCEP());

        }
    }//GEN-LAST:event_vendajButtonActionPerformed
    /**
     * Atualiza a tabela assim que a janela é aberta.
     */
    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        atualizaTable();
    }//GEN-LAST:event_formInternalFrameOpened
/**
 * Método utilizado para retornar objeto Cliente.
 * @return Cliente Cliente
 * @deprecated 
 */
    public Cliente retornaObj() {
        return cli;
    }
    /**
     * Atualiza a Tabela conTableCliente, fazendo nova busca na base de dados.
     */
    public void atualizaTable() {
        System.out.println("Atualizou Tabela Cliente");
        conClijTable.removeAll();
        conTableCliente con = new conTableCliente();
        conClijTable = con.ConsultaTabelaToda(conClijTable);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton alterarjButton;
    private javax.swing.JButton buscarjButton;
    private javax.swing.JTextField buscarjTextField;
    private javax.swing.JTable conClijTable;
    private javax.swing.JButton excluirjButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox nome_cpfjComboBox;
    private javax.swing.JButton novojButton;
    private javax.swing.JPanel opcoesjPanel;
    private javax.swing.JLabel pesquisarjLabel;
    private javax.swing.JButton vendajButton;
    // End of variables declaration//GEN-END:variables
}
